<?
	session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<script type="text/javascript" src="res/js/jquery-1.6.1.min.js"></script>
	<script type="text/javascript" src="res/js/global.js"></script>
	
	<!-- Page customized -->
	<script type="text/javascript">
	$(document).ready(function() {
		$('#query_text_box').keypress(function(event) {
				if(event.keyCode == 13) {
					doQuery();
				}
			});
		});

	function handleAjaxJson2ResultPane(data) {
		$('#result').html( '<pre>' + JSON.stringify(data, null, '\t') + '</pre>' );
	}

	function doQuery() {
		var value = $('#query_text_box').val();
		//value = trim(value);
		if(empty(value)) {
			return;
		}
		
		// call system ajax (mech)
		if (value.substr(0,2) == '$:') {
			value = value.substr(3);
			call('ajax/mech.php?' + value, handleAjaxJson2ResultPane, handleAjaxJson2ResultPane);
		} else {
			call('ajax/query.php?act=q&qs=' + value,
				function (data) {
					var rows = data.data;
					// produce result table
					var s = '<table>'
					var start = true;
					for (var _r in rows) {
						if (start) {
							s += '<tr>';
							for (var _c in rows[_r]) {
								s += '<th>' + _c + '</th>';
								start = false;
							}
							s += '</tr>';
						}
						s += '<tr>';
						for (var _c in rows[_r]) {
							s += '<td>' + rows[_r][_c] + '</td>';
						}
						s += '</tr>';
					}
					s += '</table>';

					$('#result').html(s);
				},
				function (data) {
					$('#result').html('<center><p>' + data.err + '</p></center>');
				}
			);
		}
	}

	</script>

	<link href="res/css/qt_site.css?<?php echo microtime (true); ?>"  rel="stylesheet" type="text/css" >
</head>
<body>
	<!-- top -->
	<?
		include_once('_top.php');
	?>
	<div id="content">
		<center>
			<div style="width:1000px;">
				<?
					$me = 'console';
					include_once('_nav.php');
				?>

				<!-- ERROR MESSAGE -->
				<div class="clear"></div>
				<div id="em"><span id="ajax_error_message"></span></div>

				<!-- PAGE CONTENT of W:1000px -->
				<div class="curve page_content" style="text-align:center;">
					<!-- CONSOLE -->
					<div class="right">
						<a href="javascript:void()" onclick=""><img src="res/img/connected.png" style="margin: 0px -31px -14px -3px;"></img></a>
					</div>
		            <div class="console_query_box_div">
		                q: <input class="console_query_box" id="query_text_box" type="text" placeholder="query string"/>
		            </div>
		            <div class="clear"></div>
		            <div id="result" class="console_result_box_div">
		            	<center>
		            		<p style="top: 50%;">
		            		console disconnected<br/>- -a
		            		</p>
		            	</center>
		            </div>
				</div>
			</div>
		</center>
	</div>
	<!-- foot -->
	<?
		include_once('_foot.php');
	?>
</body>
</html>